package com.oa.office.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.oa.office.biz.HeadRoutingBiz;
import com.oa.office.entity.HeadRoutingWithBLOBs;

@CrossOrigin
@Controller
@RequestMapping("/headRout")
public class HeadRoutingController {
	
	@Resource
	private HeadRoutingBiz headRoutingBiz;
	
	@RequestMapping("/list")
	public String list(Model model,@RequestParam(name="spar", defaultValue="") String spar){
		try {
			if(spar.equals("")){
				Date date = new Date();
				Calendar c = Calendar.getInstance();
				c.setFirstDayOfWeek(Calendar.MONDAY);
				c.setTime(date);
				spar=new Integer(c.get(Calendar.WEEK_OF_YEAR)-1).toString();
			};
			List<HeadRoutingWithBLOBs> headRoutList=headRoutingBiz.selectByWeek(spar);
			model.addAttribute("headRoutList", headRoutList);
			model.addAttribute("spar",spar);
			return "office/index";
		} catch (Exception e) {
			e.printStackTrace();
			return "office/nok";
		}
	}
	@RequestMapping("/find")
	public String find(){
		return "office/find";
	}
	@RequestMapping("/find1")
	public String find1(Model model,@RequestParam(name="day", defaultValue="") String day){
		HeadRoutingWithBLOBs hrwb;
		try {
			hrwb = headRoutingBiz.selectByDay(day);
			model.addAttribute("hrwb", hrwb);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date dayDate = sdf.parse(day);
			model.addAttribute("dayDate", dayDate);
			return "office/index2";
		} catch (Exception e) {
			e.printStackTrace();
			return "office/nok";
		}
	}
	@RequestMapping("/list2")
	public String list2(Model model, String spar){
		try{
			List<HeadRoutingWithBLOBs> headRoutList=headRoutingBiz.selectByWeek(spar);
			model.addAttribute("headRoutList", headRoutList);
			model.addAttribute("spar",spar);
			return "office/index";
		} catch (Exception e) {
			e.printStackTrace();
			return "office/nok";
		}
	}
	@RequestMapping("/add")
	public String add(Model model,HeadRoutingWithBLOBs hrwb,String day1){
		try {
			headRoutingBiz.insertSelective(hrwb,day1);
			return "office/ok";
		} catch (Exception e) {
			e.printStackTrace();
			return "office/nok";
		}
	}
	
}
